package com.piece.mall.controller;

import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.mall.model.OmsPaymentChannel;
import com.piece.mall.service.OmsPaymentChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = {"支付客户端管理"})
@RequestMapping("/api/mall/payment/channel")
public class OmsPaymentChannelController extends FrameWorkController {

    @Resource
    private OmsPaymentChannelService omsPaymentChannelService;

    @PostMapping("/list")
    @AclPermission(code = "pay:channel:view")
    @ApiOperation(value = "查询支付客户端列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = omsPaymentChannelService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "pay:channel:view")
    @ApiOperation(value = "查询支付客户端分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = omsPaymentChannelService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "pay:channel:view")
    @ApiOperation(value = "加载支付客户端表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        long id = Convert.toLong(getRequest().getParameter("id"), 0L);
        OmsPaymentChannel omsPaymentChannel = new OmsPaymentChannel();
        if (id != 0) {
            omsPaymentChannel = omsPaymentChannelService.findById(id);
        }
        return success(omsPaymentChannel);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "pay:channel:edit")
    @ApiOperation(value = "保存支付客户端表单数据")
    @ApiImplicitParam(value = "支付客户端数据", dataTypeClass = OmsPaymentChannel.class, type = "body")
    public AjaxResponse save() {
        OmsPaymentChannel omsPaymentChannel;
        try {
            Long id = Convert.toLong(getRequest().getParameter("id"));
            if (null != id) {
                omsPaymentChannel = omsPaymentChannelService.findById(id);
            } else {
                omsPaymentChannel = new OmsPaymentChannel();
            }
            initDB(omsPaymentChannel);
            omsPaymentChannelService.insertOrUpdate(omsPaymentChannel);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "pay:channel:delete")
    @ApiOperation(value = "删除支付客户端数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            omsPaymentChannelService.delete(Convert.toLongArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

}
